<?php
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Product_CategoryController
 *
 * @author LeVinhs
 */
include_once APPLICATION_PATH."/models/Product_Category.php";
include_once APPLICATION_PATH."/models/Product.php";
class Admin_ProductcategoryController extends Zend_Controller_Action{

    public function init() {
        $bootstrap = $this->getInvokeArg("bootstrap");
        $this->aConfig = $bootstrap->getOptions();
        $this->view->aConfig = $this->aConfig;
        $this->model = new Model_Product_Category();
        $this->modelMapper = new Model_Product_CategoryMapper();
        $this->ProductmodelMapper = new Model_ProductMapper();
    }
    public function indexAction()
    {        
    }    
    public function serviceAction(){        
        $status_filter = $this->_getParam("status","0");
        $this->_helper->layout->disableLayout();            
        foreach ($this->modelMapper->fetchAll() as $items ) 
        {        
            if($items->getParent()=="0") $parent_item ='---';
            else $parent_item=  GlobalLib::getName ('product_category', $items->getParent(), 'name');            
            if($status_filter=="1" && $items->getStatus()=="1")
            {    
                $menber[]=array(
                    'id'=>$items->getId(),
                    'name'=>$items->getName(),               
                    'parent'=>$parent_item,   
                    'order'=>$items->getOrder()                   
                ); 
            }
            else if($status_filter=="0")
            {
                $menber[]=array(
                    'id'=>$items->getId(),
                    'name'=>$items->getName(),               
                    'parent'=>$parent_item,   
                    'order'=>$items->getOrder(),
                    'status'=>$items->getStatus()
                );  
            } 
        }
        echo json_encode($menber);
        exit();
    }
    public function listAction() {                
    }
    public function addAction()
    {
        if ($this->getRequest()->isPost()) {               
            $redirectUrl = $this->aConfig["site"]["url"] . "admin/productcategory/list";
            if (isset($_POST["id"])) {
                $this->model->setId($_POST["id"]);
            }
            if (isset($_POST["name"])) {
                $this->model->setName($_POST["name"]);
            }
            if (isset($_POST["parent"])) {
                $this->model->setParent($_POST["parent"]);
            }
            else{
                $this->model->setParent('0');
            }
            if (isset($_POST["order"])) {
                $this->model->setOrder($_POST["order"]);
            }
            if (isset($_POST["status"])) {
                $status = 1;
            } else {
                $status = 0;
            }            
            $this->model->setStatus($status);
            $this->model->setCreated(date("Y/m/d H:i:s"));
            $this->model->setCreated_By(GlobalLib::getLoginId());
            $this->model->setModified(date("Y/m/d H:i:s"));
            $this->model->setModified_By(GlobalLib::getLoginId());
            $this->modelMapper->save($this->model);
            $this->_redirect($redirectUrl);
        }
        $this->view->item = $this->model;
    }    
    public function editAction() {
        $id= $this->_getParam("id","");
        $redirectUrl=$this->aConfig["site"]["url"]."admin/productcategory/list";
        if(empty($id)){
            $this->_redirect($redirectUrl);
        }
        $this->modelMapper->find($id,$this->model);
        $getId=$this->model->getId();
        if(empty($getId)){
             $this->_redirect($redirectUrl);
        }
        if($this->getRequest()->isPost()){
            if(isset($_POST["id"])){
                $this->model->setId($_POST["id"]);
            }
            if(isset($_POST["name"])){
                $this->model->setName($_POST["name"]);
            }
            if(isset($_POST["order"])){
                $this->model->setOrder($_POST["order"]);
            }
            if(isset($_POST["status"])){
                $status=1;
            } else {
                $status=0;
            }
            if (isset($_POST["parent"])) {
                $this->model->setParent($_POST["parent"]);
            }            
            $this->model->setStatus($status);
            $this->model->setModified(date("Y/m/d H:i:s"));
            $this->model->setModified_By(GlobalLib::getLoginId());
            $this->modelMapper->save($this->model);
            $this->_redirect($redirectUrl);
        }        
        $this->view->item=$this->model;
    }
    public function delconfirmAction()
    {
        $id_filter = $this->_getParam("id","0");
        $this->_helper->layout->disableLayout();
        $count =0;
        foreach ($this->modelMapper->fetchAll() as $items ) 
        {        
            if($items->getParent()==$id_filter)
            {
                $count++;
            }
        }                
        $model_product = new Model_ProductMapper();
        foreach ($model_product->fetchAll() as $items ) 
        {        
            if($items->getProduct_Cat_Id()==$id_filter)
            {
                $count++;
            }
        }
        echo $count;
        exit();
    }
    public  function confirmdeleteAction()
    {
        $id = $this->_getParam("id","0");
        $count = 0;
        foreach($this->ProductmodelMapper->fetchAll() as $item)
        {
            if($item->getProduct_Cat_Id()==$id) $count= $count+1;
        }
        foreach($this->modelMapper->fetchAll() as $item)
        {
            if($item->getParent()==$id) $count = $count+1;
        }
        echo $count;
        exit();
    }
    public function deleteAction(){
        $id= $this->_getParam("id","");
        $redirectUrl=$this->aConfig["site"]["url"]."admin/productcategory/list";               
        foreach($this->modelMapper->fetchAll() as $item)
        {
            if($item->getParent()==$id) $this->modelMapper->delete($item->getId());
            foreach($this->ProductmodelMapper->fetchAll() as $pitem)
            {
                if($pitem->getProduct_Cat_Id()==$item->getId()) $this->ProductmodelMapper->delete($pitem->getId());
            }
        }
        foreach($this->ProductmodelMapper->fetchAll() as $item)
        {
            if($item->getProduct_Cat_Id()==$id) $this->ProductmodelMapper->delete($item->getId());
        }
        $this->modelMapper->delete($id);
        $this->_redirect($redirectUrl);
    }
}
